<template>
	<view>
		<!--  v-if="patientList.length>=1" -->
		<view  class="medicalCard" >
		
			<view class="card" v-for="(item,index) in patientList" :key="item.id">
				<view class="top">
					就诊卡
				</view>
				<view class="content">
					<view class="left">
						<view class="name">
							姓名: <span>{{item.name}}</span>
						</view>
						<view class="sex">
							性别: <span>{{item.sex==1?'女':'男'}}</span>
						</view>
						<view class="tel">
							电话: <span>{{item.tel}}</span>
						</view>
						<view class="address">
							住址: <span>{{item.address}}</span>
						</view>
					</view>
					<view class="right">
						<view class="userId">
							用户ID: <span>{{item.userId}}</span>
						</view>
						<view class="IDNum">
							身份证: <span>{{item.cardId}}</span>
						</view>
						<view class="birthday">
							出生年月: <span>{{item.birthday}}</span>
						</view>
						<view class="changeCard">
							<span @click="changeCard(item)">修改信息</span>
						</view>
					</view>
				</view>
				
			</view>
			
			<!-- <view class="card" >
				<view class="top">
					就诊卡
				</view>
				<view class="content">
					<view class="left">
						<view class="name">
							姓名: <span>star</span>
						</view>
						<view class="sex">
							性别: <span>男</span>
						</view>
						<view class="tel">
							电话: <span>13212341234</span>
						</view>
						<view class="address">
							家庭住址: <span>湖北武汉</span>
						</view>
					</view>
					<view class="right">
						<view class="userId">
							用户ID: <span>2</span>
						</view>
						<view class="IDNum">
							身份证: <span>"210211199909090014</span>
						</view>
						<view class="birthday">
							出生年月: <span>2024 - 04 - 03</span>
						</view>
						<view class="changeCard">
							<span @click="showAddForm">修改信息</span>
						</view>
					</view>
				</view>
				
			</view> -->
			
			<view class="addMedicalCard">
				<button @tap="showAddForm">新增就诊卡</button>
			</view>
		</view>
		
		<!-- 没有就诊卡时显示 -->
		<view v-if="patientList.length<1" class="nothing">
			<img src="https://img2.baidu.com/it/u=526657140,2374443277&fm=253&fmt=auto&app=138&f=JPEG" alt="还没有就诊卡">
			<view class="addPatient">
				<button @click="showAddForm">添加我的第一张就诊卡</button>
			</view>
		</view>
		
		<!-- 遮罩层 -->
		<view class="addFormBox" v-if="show" @click="show=false">
			<view class="formBox" @tap.stop>
				<view class="ui-all">
					<view class="avatar" @tap="avatarChoose">
						<view  class="imgAvatar">
							<view class="iavatar" :style="'background: url('+avater+') no-repeat center/cover #eeeeee;'"></view>
						</view>
						<text v-if="avater">修改头像</text>
						<text v-else>上传头像</text>
						
					</view>
					<view class="ui-list">
						<text>姓名</text>
						<input type="text" :placeholder="value" :value="nickName" @input="bindnickName" placeholder-class="place" />
					</view>
					<view class="ui-list">
						<text>手机号</text>
						<input type="tel" :placeholder="value" :value="mobile" @input="bindmobile" placeholder-class="place" />
					</view>
					<view class="ui-list right">
						<text>性别</text>
						<picker @change="pickerChangeData" mode='selector' range-key="name" :value="index" :range="sex">
							<view class="picker">
								{{sex[index].name}}
							</view>
						</picker>
					</view>
					<view class="ui-list">
						<text>身份证</text>
						<input type="text" :placeholder="value" :value="IDNum" @input="bindIDNum" placeholder-class="place" />
					</view>
					
					<view class="ui-list right">
						<text>常住地</text>
						<view class="address">
							<span @click="toAddress(1)">{{provinvial?provinvial:'选择省份'}}</span>
							<span @click="toAddress(2)">{{city?city:'选择市区'}}</span>
							<span @click="toAddress(3)">{{area?area:'选择县区'}}</span>
						</view>
					</view>

					<view class="ui-list right">
						<text>生日</text>
						<picker mode="date" :value="date" @change="bindDateChange">
							<view class="picker">
								{{date}}
							</view>
						</picker>
					</view>
					
					<button class="save" @tap="savaInfo"> 保 存 </button>
				</view>
				
			</view>
		</view>
		
		
	</view>
</template>

<script>
	
	import{getPatientList,addPatientApi} from '../../../api/hospitalApi.js'
	import {mockPatientList,mockReservation} from '../../../mock/hospital.js';
	export default {
		data() {
			return {
				patientList:[],
				show:false,
				// 表单要用的数据:
				value: '请填写',
				sex: [{
					id: 1,
					name: '男'
				}, {
					id: 2,
					name: '女'
				}],
				index: 0,
				provinvial:'',//省
				city:'',//市
				area:'',//区
				date: '请填写',
				avater: '../../../static/user.png',//头像
				url: '',
				nickName: '',
				mobile: '',
				headimg: '',
				IDNum:'',
				userInfo:{},
				state:0,
				infoId:0,
				userId:0,
				userInfoId:0
				
			}
		},
		methods: {
			// toAddress跳转到地区选择页面
			toAddress(id){
				console.log("地区选择id:",id);
				console.log();
				uni.navigateTo({
					url:'/pages/outpatient/ceShi/ceShi?id='+id
				})
			},
			
			// 表单填写使用的函数
			pickerChangeData(e) {
				this.index = e.detail.value;
				
			},
			
			bindDateChange(e) {
				this.date = e.detail.value;
				
			},
			bindnickName(e) {
				this.nickName = e.detail.value;
				
			},
			bindmobile(e) {
				this.mobile = e.detail.value;
				
			},
			bindIDNum(e) {
				this.IDNum = e.detail.value;
				
			},
			avatarChoose() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// tempFilePath可以作为img标签的src属性显示图片
						that.imgUpload(res.tempFilePaths);
						const tempFilePaths = res.tempFilePaths;
					}
				});
			},
			
			savaInfo() {
				let that = this;
				let nickname = that.nickName;
				let headimg = that.headimg;
				let gender = that.index;
				let mobile = that.mobile;
				let sex = that.sex;
				let region = [that.provinvial,that.city,that.area];
				let birthday = that.date;
				let updata = {};
				let idNum = this.IDNum;
				
				if (!nickname) {
					uni.showToast({
						title: '请填写昵称',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				this.userInfo.name = nickname;
				if (!headimg) {
					headimg = that.avater;//头像
				}
				this.userInfo.imgUrl = headimg;
				this.userInfo.sex = gender;
				if (that.isPoneAvailable(mobile)) {
					this.userInfo.tel = mobile;
				} else {
					uni.showToast({
						title: '手机号码有误，请重填',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (!(region[0]&&region[1]&&region[2])) {
					uni.showToast({
						title: '请选择常住地',
						icon: 'none',
						duration: 2000
					});
					return;
				} else {
					updata.province = region[0];
					updata.city = region[1];
					updata.area = region[2];
				}
				if (birthday == "请选择") {
					uni.showToast({
						title: '请选择生日',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				this.userInfo.birthday = birthday;
				that.updata(updata);
				this.userInfo.cardId = idNum;//身份证
				
				console.log('updata::::',updata);
				this.userInfo.address = (updata.city == "市辖区")?(updata.province+updata.area):(updata.city+updata.area);
				this.state?(this.userInfo.userId=this.userId):(this.userInfo.userId = (this.userId-0)+1);
				this.state?(this.userInfo.id = this.infoId-1):(this.userInfo.id = this.patientList.length);
				console.log("userInfo==>",this.userInfo);
				
				if(this.state != 1){
					//调接口新增就诊卡
					this.addPatient(this.userInfo);
					
				}else{
					// 修改就诊卡
					console.log("就诊卡列表==>",this.patientList);
					console.log("修改的",this.patientList[this.infoId]);
					this.patientList[this.infoId] = this.userInfo;
					this.state = 0;
				}
				
				
				this.show = false;
				
			},
			isPoneAvailable(poneInput) {
				var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				if (!myreg.test(poneInput)) {
					return false;
				} else {
					return true;
				}
			},
			async updata(datas) {
				//传后台
				
			},
			imgUpload(file) {
				let that = this;
				uni.uploadFile({
					header: {
						Authorization: uni.getStorageSync('token')
					},
					// url:'/api/upload/image', //需传后台图片上传接口
					filePath: file[0],
					name: 'file',
					formData: {
						type: 'user_headimg'
					},
					success: function(res) {
						var data = JSON.parse(res.data);
						data = data.data;
						that.avater = that.url + data.img;
			
						that.headimg = that.url + data.img;
			
						
					},
					fail: function(error) {
						console.log(error);
					}
				});
			},
			
			// 修改就诊卡
			changeCard(data){
				console.log("修改的data:",data);
				this.state = 1;
				this.infoId = data.id-1;
				this.userId = data.userId;
				console.log("infoId:",this.infoId);
				this.showAddForm();
				
			},
			
			
			// 显示遮罩层
			showAddForm(){
				this.show = true;
			},
			
			// 添加就诊卡
			async addPatient(info){
				console.log("触发了新增事件");
				let res2 = await addPatientApi(
					{
					"address": info.address,
					"birthday": info.birthday,
					"cardId": info.cardId,
					"name": info.name,
					"sex": info.sex,
					"tel": info.tel
					}
				);
				console.log("新增就诊卡的res2:==>",res2);
				if(res2.code == 200){
					uni.showToast({
						title: res2.msg,
						icon: 'none',
						duration: 2000
					});
				}else{
					console.log("info==>",info);
					this.patientList.push(info);
					console.log("新增后的patientList:",this.patientList);
				}
			},
			
			// 获取就诊卡列表
			async getPatientList() {
				let res = await getPatientList();
				console.log("就诊卡列表:res",res);
				if(res.code == 200){
					this.patientList = res.rows;
				}else{
					this.patientList = mockPatientList().rows;
				}
			}
		},
		onLoad() {
			this.getPatientList();
			let provinvial = uni.getStorageSync('provinvial').name;
			let city = uni.getStorageSync('city').name;
			let area = uni.getStorageSync('area').name;
			this.provinvial = provinvial;
			this.city = city;
			this.area = area;
			
			// console.log(lcwSelectAddress);
		},
		onShow(){
			let provinvial = uni.getStorageSync('provinvial').name;
			let city = uni.getStorageSync('city').name;
			let area = uni.getStorageSync('area').name;
			this.provinvial = provinvial;
			this.city = city;
			this.area = area;
		}
	}
</script>

<style lang="scss" scoped>
	// 就诊卡样式
	.card{
		width: 95%;
		margin: 10rpx auto;
		overflow: hidden;
		border-radius: 16rpx;
		box-sizing: border-box;
		padding: 20rpx;
		background-color: #7effdf;
		
		.top{
			width: 100%;
			text-align: center;
			font-size: 18px;
			line-height: 26px;
			padding: 10rpx 0;
			font-weight: 600;
			letter-spacing: 2em;
		}
		.content{
			width: 100%;
			display: flex;
			justify-content: space-between;
			font-size: 16px;
			
			.left &view, .right & view{
				margin: 10rpx 0;
				overflow: hidden;
				// line-height: 30px;
			}
			& span{
				color: #a00;
				line-height: 30px;
			}
			.changeCard{
				width: 100%;
				text-align: center;
				& span{
					color: #f4f4f4;
					padding: 12rpx 50rpx;
					border-radius: 20rpx;
					background-color: #eb544a;
				}
			}
		}
	}
	
	// 填写信息模块的样式
	.ui-all {
		padding: 20rpx 40rpx;
	
		.avatar {
			width: 100%;
			text-align: left;
			padding: 20rpx 0;
			border-bottom: solid 1px #f2f2f2;
			position: relative;
	
			.imgAvatar {
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
				display: inline-block;
				vertical-align: middle;
				overflow: hidden;
	
				.iavatar {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
	
			text {
				display: inline-block;
				vertical-align: middle;
				color: #000;
				font-size: 28rpx;
				margin-left: 40rpx;
			}
		}
	
		.ui-list {
			width: 100%;
			text-align: left;
			padding: 20rpx 0;
			border-bottom: solid 1px #f2f2f2;
			position: relative;
	
			.address{
				width: 100%;
				height: 60rpx;
				font-size: 14px;
				line-height: 60rpx;
				margin-top: 20rpx;
				display: flex;
				justify-content: space-around;
				
				& span{
					text-align: center;
					padding: 0 8rpx;
					border: 1px solid #000;
					border-radius: 20rpx;
				}
			}
	
			text {
				color: #000;
				font-size: 28rpx;
				display: inline-block;
				vertical-align: middle;
				min-width: 150rpx;
			}
	
			input {
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
			}
			button{
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				background: none;
				margin: 0;
				padding: 0;
				&::after{
					display: none;
				}
			}
			picker {
				width: 90%;
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				position: absolute;
				top: 30rpx;
				left: 150rpx;
			}
	
			textarea {
				color: #030303;
				font-size: 30rpx;
				vertical-align: middle;
				height: 150rpx;
				width: 100%;
				margin-top: 50rpx;
			}
	
			.place {
				color: #333;
				font-size: 28rpx;
			}
		}
	
		.right:after {
			content: ' ';
			width: 20rpx;
			height: 20rpx;
			border-top: solid 1px #030303;
			border-right: solid 1px #030303;
			transform: rotate(45deg);
			-ms-transform: rotate(45deg);
			/* IE 9 */
			-moz-transform: rotate(45deg);
			/* Firefox */
			-webkit-transform: rotate(45deg);
			/* Safari 和 Chrome */
			-o-transform: rotate(45deg);
			position: absolute;
			top: 40rpx;
			right: 0;
		}
	
		.save {
			background: #030303;
			border: none;
			color: #ffffff;
			margin-top: 40rpx;
			font-size: 28rpx;
		}
	}
	
	
	
	
	// 填写资料信息时的遮罩层的样式
	.addFormBox,.formBox{
		box-sizing: border-box;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
	}
	.addFormBox{
		width: 100%;
		height: 100%;
		position: fixed;
		padding: 50rpx;
		background-color:rgba(128,128,128,.7);
	}
	.formBox{
		width: 80%;
		height: 80%;
		padding: 20rpx;
		position: absolute;
		border-radius: 30rpx;
		background-color: rgba(2,189,173,1);
	}
	
	
	// 没有就诊卡时显示的块元素的样式
	.nothing{
		width: 100%;
		
		& img{
			display: block;
			width: 100%;
		}
		
	}
	.nothing .addPatient,.addMedicalCard{
		width: 100%;
		text-align: center;
		
		padding: 30rpx 0;
		
		& button{
			display: inline-block;
			font-size: 20px;
			line-height: 40px;
			color: #000;
			background-color: #fff;
			border: 1px solid #48006c;
		}
	}
	// 新建就诊卡的按钮所在区域的样式
	.addMedicalCard button{
		border: 0;
		color: #d50a0a;
		background: linear-gradient(to right,  rgba(183,222,237,1) 0%,rgba(113,206,239,1) 50%,rgba(33,180,226,1) 51%,rgba(183,222,237,1) 100%);

	}
	
</style>
